<?php
/**
* Admin Authorize.net Controller
*
* This handles all AJAX requests from the Admin GUI Authorize.net Configuration page.
* These methods will create an AJAX response containing JSON data. The JSON
* data is read by the JS code and rendered into interface.
*
* @access 	public
* @author 	Rob, Valentin
* @copyright	ResMania 2009 all rights reserved.
* @version	1.0
* @link		http://developer.resmania.com/api
* @since  	05-2009
*/
class RM_Admin_AuthorizeNetController extends RM_Controller
{
    /**
     * This loads the Authorize.net configuration, the data is retrieved using the
     * RM_AuthorizeNet class
     *
     * @return 	json    json array information containing Authorize.net information
     */
    public function configJsonAction(){

        $paypalObj = new RM_AuthorizeNet;
        $settings = $paypalObj->getSettings()->toArray();

        $json = new stdClass;
        $json->settings = $settings;

        return array(
            'data' => $json
        );
    }

    /**
     * This saves the authorize.net settings from the plugin->authorizenet setting page
     *
     * @return 	json    success value true/false
     */
    public function updateJsonAction(){

        $id = $this->_getParam('id');
        $api_id = $this->_getParam('api_id');
        $trans_key = $this->_getParam('trans_key');
        $pre_authorize = $this->_getParam('pre_authorize');
        $test_mode = $this->_getParam('test_mode');
        $default = $this->_getParam('default');

        $authorizenetObj = new RM_AuthorizeNet;

        // find the current record from the DB
        $details = $authorizenetObj->find($id)->current();

        $status = 'true'; // set this true the if we have a problem we set it false

        if (!$details) {
            $status = 'false';
        } else {

            $details->api_id = $api_id;
            $details->trans_key = $trans_key;
            $details->pre_authorize = $pre_authorize;
            $details->test_mode = $test_mode;
            $details->defaultplugin = $default;
            $result = $details->save();

            if ($result!='1') $status = 'false';
        }
        
        return array(
            'data' => array('success' => $status)
        );

    }
}